const app = getApp()
import dateFormat from '../../utils/dateFormat';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_list: []
  },

  cancel_order(e) {
    let order_id = e.currentTarget.dataset.item.order_id
    app.request("/user/cancel_activity_order", {
      method: "post",
      data:{
        order_id
      }
    }).then(res=>{
      console.log(res);
      this.fetch()
    })
  },

  pay(e) {
    console.log(e);
    let order_id = e.currentTarget.dataset.item.order_id
    let type = e.currentTarget.dataset.item.type
    this.wxpay(order_id, type)
  },
  // 支付
  wxpay(order_no, type) {
    let that = this
    app.request("/user/activity_pay", {
      method: "POST",
      data: {
        order_no,
        type
      }
    }).then(res => {
      console.log(res, "发起支付");
      let {
        timeStamp,
        nonceStr,
        signType,
        paySign
      } = res.data.data
      let packages = res.data.data.package
      let out_trade_no = res.data.order_id
      wx.requestPayment({
        timeStamp: timeStamp + '',
        nonceStr: nonceStr,
        package: packages,
        signType: signType,
        paySign: paySign,
        fail(err) {
          console.log(err)
          wx.navigateTo({
            url: '/pages/order_list_activity/order_list_activity',
          })
        },
        success(res) {
          console.log(res, "支付成功");
          //查询订单
          app.request("/user/pay/activity_search", {
            method: "POST",
            data: {
              out_trade_no
            }
          }).then(res => {
            console.log(res, "订单查询成功");
            that.fetch()
          })
        }
      })
    })
  },

  fetch() {
    app.request("/user/get_activity_orders", {}).then(res => {
      console.log(res);
      let order_list=res.data.data
      order_list.forEach(item=>{
        item.add_time = dateFormat(" YYYY-mm-dd HH:MM", item.add_time*1000)
      })
      this.setData({
        order_list
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.fetch()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})